<?php
/* class that manages the image stock */
class imagestock{
	/* defines the path where the images are */
	private $image_path = CONFIG_IMAGE_PATH;

	/* delete an image from disk */
	public function deleteImage($filename){
		// unlink deletes the file from disk
		unlink($this->image_path."/".$filename);
	}
	
	/* checks whether a file has been uploaded 
	 * and if so it processes the file and moves 
	 * it to the correct destination on the disk */
	public function checkUpload(){
		if(array_key_exists("imageFile",$_FILES)){
			// make sure it is an image file
			if(substr($_FILES["imageFile"]["type"],0,5)=="image"){
				// move the file to the image path on the disk
				move_uploaded_file($_FILES["imageFile"]["tmp_name"], 
					$this->image_path."/".$_FILES["imageFile"]["name"]);
			}
		}
	}
	
	/* returns a list with all images */
	public function getImageList(){
		$result = array();

		/* open the file handle */
		$r = 0;
		if ($handle = opendir($this->image_path)) {
			/* walk through all files in the dir */
			while (false !== ($file = readdir($handle))) {
				// don't use the dir links
				if($file != "." && $file != ".."){
					// generate a proper title out of the filename
					$filetitle = str_replace(substr($file,strrpos($file,".")), "", 
											substr($file,strrpos($file, "/")));
					$title = str_replace("-", " ", $filetitle);
					$title = str_replace("-", " ", $title);
					
					// add the data to the result
					$result[$r] = array();
					$result[$r]["name"] = $file;
					$result[$r]["ext"] = substr($file,strrpos($file,".")+1);
					$result[$r]["url"] = "/img/".substr($file,strrpos($file, "/"));
					$result[$r]["filetitle"] = $filetitle;
					$result[$r]["title"] = $title;
					$result[$r]["thumb_url"] = "/img/".$result[$r]["filetitle"]
													."_200.".$result[$r]["ext"];
					$result[$r]["id"] = hash("md5",$file);
					$r++;
				}
			}
		}

		return $result;
	}
}
?>